(function(){
	'use strict';

    var app = angular.module('myApp.i18n-module');
	
    app.factory( 'i18nService', [ '$http', '$window', function ( $http, $window ) {
    	var i18nService = {};
    	
    	i18nService.loadByDomain = function( domain ) {
    		var i18n = angular.fromJson( $window.sessionStorage['i18n'] );
    		if( i18n != undefined && i18n[domain] != undefined ) return domain;
    		
    		$http( { method: 'GET', url: '/translations/loadByDomain', params: { domain: domain } } )
    		.then( function( response ){
    			if( response.data.error == undefined ){
    				if( response.data.length == 0 ){
    					console.log( 'There is no translations for domain("' + domain + '")' );
    					response.data = {};
    				}
    				
    				i18n = i18n == undefined ? {} : i18n;
    				i18n[domain] = response.data;
    				$window.sessionStorage['i18n'] = JSON.stringify( i18n );
    			}else{
    				console.log( 'Error: ' + response.error );
    			}
    		}, function(){
    			console.log( 'Error loading domain("' + domain + '") translations' );
    		} );
    		
    		return domain;
    	};
    	
    	i18nService.loadTranslation = function( domain, key ) {
    		$http( { method: 'GET', url: '/translations/loadTranslation', params: { domain: domain, key: key } } )
    		.then( function( response ){
    			
    		}, function(){
    			console.log( 'Error loading translation with domain = "' + domain + ', key' + key );
    		} );
    	};
    	
    	return i18nService;
    } ] );
    
})();